/**
 * Created by gongxun on 17/1/18.
 */

var PszTableStatus = function() {
    var that = {};

    //上次的牌桌印象
    that.mOldTableStatus = null;

    //牌桌映像
    that.mTableStatus = null;

    //录像msg
    that.replayMsgs = null;

    //录像type
    that.replayTypes = null;

    //是否是断线重练
    that.isReconnect = false;

    //设置牌桌映像
    that.setTableStatus = function(tableStatus){
        that.mTableStatus = tableStatus;

        //记录是否是断线重连
        if (that.mTableStatus.Step != 0){
            that.isReconnect = false;
        }

        //在牛牛里面才触发
        if (mo.sceneHelper.currentLayerName != "psz"){
            return;
        }

        //特殊处理
        // mo.pszTable.handlerJuesaunInfo(tableStatus);

        that.updateTableStatus();

        that.mOldTableStatus = tableStatus;
    };

    that.setReconnectStatus = function(tableStatus){
        that.mTableStatus = tableStatus;
        that.mOldTableStatus = tableStatus;

        //记录是否是断线重连
        if (that.mTableStatus.Step == 0){
            that.isReconnect = true;
        }

        //更新用户信息
        that.updateUserInfo();

        //断线重连
        that.setReconnet();

        //ui状态
        that.setUIShowStatus();
    };

    that.updateTableStatus = function(){
        //更新用户信息
        that.updateUserInfo();

        //是否有玩家看牌
        if (that.getIsPlayerSeeCard(that.mOldTableStatus, that.mTableStatus)){
            return;
        }

        //更新动画
        that.updateStep();

        //ui状态
        that.setUIShowStatus();
    };

    that.updateUserInfo = function(){
        //牌局结束以后（剩余次数为0，step为0） 不显示用户ui变化
        if (that.mTableStatus.LeftPlayTimes == 0 && that.mTableStatus.Step == 0){
            return;
        }

        //收到大结算了 不处理用户变化
        if (mo.pszTable.mPszTableRoomEndLayer != null){
            return;
        }

        if (that.mTableStatus.TableID){
            var infos = [null, null, null, null, null];
            for (var seat = 0; seat < 5; seat++){
                var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                infos[seat] = that.mTableStatus["SeatUser" + (logicSeat + 1)];
            }
            mo.pszTable.mPszTableUILayer.setTablePlayerInfos(infos);
        }else{
            //录像可能没有
        }
    };

    //更新牌局信息
    that.updateStep = function(){
        if (that.mTableStatus.Step == 0){

        }else if (that.mTableStatus.Step == 1){
            var lastAction = that.mTableStatus.LastAction;
            var lastActionSeat = that.mTableStatus.LastActionSeat;
            if (lastAction == null){
                //发牌
                var seats = [false, false, false, false, false];
                var coins = [null, null, null, null, null];
                var cardNums = [[], [], [], [], []];
                for (var seat = 0; seat < 5; seat++){
                    var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                    var seatuser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
                    if (seatuser != null){
                        seats[seat] = true;
                        coins[seat] = seatuser.PayMoney;
                        if (seatuser.HandCards.length == 3){
                            cardNums[seat] = seatuser.HandCards;
                        }
                    }
                }
                mo.pszTable.fapai(seats, coins, cardNums);
            }else {
                var viewSeat = that.getViewForLogic(lastActionSeat - 1, that.mTableStatus.MySeat - 1);
                if (lastAction.code == 1 || lastAction.code == 2 || lastAction.code == 3){
                    //下注
                    mo.pszTable.xiazhu(viewSeat, lastAction.PayNumMin);

                    //音效
                    mo.soundHelper.playEffectFollow();
                    var sex = that.getSexForLogicSeat(lastActionSeat - 1);

                    if (lastAction.code != 3){
                        mo.soundHelper.playPszPeopleGenzhu(sex);
                    }else{
                        mo.soundHelper.playPszPeopleJiazhu(sex);
                    }
                }else if (lastAction.code == 4){
                    //比牌
                    var fromSeatLogic = lastAction.PayNumMax;
                    var toSeatLogic = lastAction.PayNumMin;
                    var formSeat = that.getViewForLogic(fromSeatLogic - 1, that.mTableStatus.MySeat - 1);
                    var toSeat = that.getViewForLogic(toSeatLogic - 1, that.mTableStatus.MySeat - 1);
                    var winSeat = formSeat;
                    var loseSeat = toSeat;
                    if (that.mTableStatus["SeatUser" + fromSeatLogic] != null){
                        if (that.mTableStatus["SeatUser" + fromSeatLogic].State == 1){
                            winSeat = formSeat;
                            loseSeat = toSeat;
                        }else {
                            winSeat = toSeat;
                            loseSeat = formSeat;
                        }
                    }

                    var cardNumss0 = that.mTableStatus["SeatUser" + fromSeatLogic].HandCards;
                    var cardNumss1 = that.mTableStatus["SeatUser" + toSeatLogic].HandCards;
                    mo.pszTable.bipai(formSeat, toSeat, winSeat, cardNumss0, cardNumss1);

                    //比牌输
                    if (loseSeat == 0){
                        mo.pszTable.mPszCardLayer.setInhandCardStatus(3, 3.5);
                    }else{
                        mo.pszTable.setPlayerSeatInhandStatus(loseSeat, 2, 3.5);
                    }

                    //音效
                    var sex = that.getSexForLogicSeat(lastActionSeat - 1);
                    mo.soundHelper.playPszPeopleBipai(sex);
                }else if (lastAction.code == 0){
                    //看牌 不存在这里拉
                }else if (lastAction.code == 5){
                    //弃牌
                    var viewSeat = that.getViewForLogic(lastActionSeat - 1, that.mTableStatus.MySeat - 1);
                    var seatUser = that.mTableStatus["SeatUser" + lastActionSeat];
                    var status = 1;
                    var cardNums = seatUser.HandCards;
                    if (cardNums.length >= 3){
                        if (cardNums[0] == 0 || cardNums[1] == 0 || cardNums[2] == 0){
                            cardNums = [];
                        }
                    }
                    if (seatUser.IsSeeHandCards == 0){
                        cardNums = [];
                    }

                    mo.pszTable.mPszCardLayer.setInhandCardForSeat(viewSeat, status, cardNums);

                    if (viewSeat == 0){
                        mo.pszTable.mPszCardLayer.setInhandCardStatus(4);
                    }else{
                        mo.pszTable.setPlayerSeatInhandStatus(viewSeat, 3);
                    }

                    //音效
                    mo.soundHelper.playEffectGiveUp();
                    var sex = that.getSexForLogicSeat(lastActionSeat - 1);
                    mo.soundHelper.playPszPeopleFangqi(sex);
                }
            }
        }
    };

    that.setReconnet = function(){
        var tableStatus = mo.pszTableStatus.mTableStatus;
        if (tableStatus.Step == 1){
            //游戏中就从新绘制
            mo.pszTable.mPszCardLayer.reset();
            mo.pszTable.mPszTableUILayer.reset();

            //自己的用户印象
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];

            //自己是否看牌
            var myIsSeeCard = (mySeatUser.IsSeeHandCards != null && mySeatUser.IsSeeHandCards != 0);

            //玩家手牌
            var seats = [false, false, false, false, false];
            var cardStatuss = [0, 0, 0, 0, 0];
            var myCardNums = [];
            var chipAll = 0;
            for (var seat = 0; seat < 5; seat++){
                var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                var seatuser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
                if (seatuser != null){
                    //显示牌
                    seats[seat] = true;

                    //显示是否离桌
                    if (seatuser.State != 1){
                        cardStatuss[seat] = 1;
                    }

                    if (seat == 0 && seatuser.IsSeeHandCards){
                        myCardNums = seatuser.HandCards;
                    }

                    chipAll += seatuser.PayMoney;
                }
            }
            mo.pszTable.mPszCardLayer.setInhandCard(seats, cardStatuss, myCardNums);

            //桌面筹码
            mo.pszTable.mPszCardLayer.setTableChips(chipAll);
            mo.pszTable.mPszTableUILayer.fapai(seats);

            //自己手牌状态
            if (mySeatUser.State == 1){
                if (myIsSeeCard){
                    var cardType = that.getCardType(mySeatUser.Cardtype);
                    mo.pszTable.mPszCardLayer.setInhandCardType(cardType);

                    mo.pszTable.mPszCardLayer.setInhandCardStatus(0);
                }else{
                    if (that.getMyIsSeeCard()){
                        mo.pszTable.mPszCardLayer.setInhandCardStatus(2);
                    }else{
                        mo.pszTable.mPszCardLayer.setInhandCardStatus(1);
                    }
                }
            }else{
                //是否是自己弃牌 (不是自己弃牌就是比牌输了)
                var isQipai = false;
                if (mySeatUser.OldActionList.length > 0) {
                    var oldAction = mySeatUser.OldActionList[mySeatUser.OldActionList.length - 1];
                    if (oldAction.code == 5){
                        isQipai = true;
                    }
                }
                if (isQipai){
                    mo.pszTable.mPszCardLayer.setInhandCardStatus(4);
                }else{
                    mo.pszTable.mPszCardLayer.setInhandCardStatus(3);
                }
            }

            //显示其他玩家手牌状态 显示手牌状态 0不显示 1已看牌 2比牌输了 3弃牌
            var inhandStatuss = [0, 0, 0, 0, 0];
            for (var seat = 1; seat < 5; seat++){
                var logicSeat = mo.pszTableStatus.getLogicForView(seat, mo.pszTableStatus.mTableStatus.MySeat - 1);
                var seatUser = mo.pszTableStatus.mTableStatus["SeatUser" + (logicSeat + 1)];
                if (seatUser != null){
                    if (seatUser.State == 1){
                        //在打牌中
                        if (seatUser.IsSeeHandCards){
                            inhandStatuss[seat] = 1;
                        }
                    }else{
                        if (seatUser.OldActionList.length > 0){
                            var oldAction = seatUser.OldActionList[seatUser.OldActionList.length - 1];
                            if (oldAction.code == 5){
                                inhandStatuss[seat] = 3;
                            }else{
                                inhandStatuss[seat] = 2;
                            }
                        }
                    }
                }
            }
            mo.pszTable.setPlayerInhandStatus(inhandStatuss);
        }
    };

    //设置ui该显示的状态
    that.setUIShowStatus = function(){
        //牌局信息
        mo.pszTable.updateLastPlayTimes();

        if (that.mTableStatus.Step == 0){
            //未开始
            mo.pszTable.mPszTableUILayer.intoShowIndex(11);

            //停止倒计时
            mo.pszTable.stopTimeLeft();
        }else if (that.mTableStatus.Step == 1){
            //游戏中
            mo.pszTable.mPszTableUILayer.intoShowIndex(20);

            //告诉ui牌桌状态变化
            mo.pszTable.setTableStatusChange();

            //该谁出牌
            var seat = that.getCurrentPlayer();
            mo.pszTable.setCurrentSeat(seat);

            //自己的用户印象
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];

            //自己是否看牌
            var myIsSeeCard = (mySeatUser.IsSeeHandCards != null && mySeatUser.IsSeeHandCards != 0);

            //自己出牌按钮状态
            if (seat == 0){
                //跟注的分数
                var genzhuScore = that.getMyGenZhuScore();
                var bipaiScore = that.getMyBipaiScore();
                mo.pszTable.mPszTableUILayer.setBetBtnStatus(mySeatUser.NewActionList, genzhuScore, bipaiScore);
            }

            //更新筹码信息
            var chips = [null, null, null, null, null];
            var chipAll = 0;
            for (var i = 0; i < 5; i++){
                var logicSeat = mo.pszTableStatus.getLogicForView(i, mo.pszTableStatus.mTableStatus.MySeat - 1);
                var seatUser = mo.pszTableStatus.mTableStatus["SeatUser" + (logicSeat + 1)];
                if (seatUser != null){
                    var payMoney = seatUser.PayMoney == null ? 0 : seatUser.PayMoney;
                    chips[i] = payMoney;
                    chipAll += payMoney;
                }
            }
            mo.gameHelper.setTextString(mo.pszTable.Text_chipAll, "" + chipAll);
            mo.pszTable.mPszTableUILayer.setPlayerChips(chips);

            //显示牌桌筹码
            mo.pszTable.Image_chipAll.setVisible(true);

            //显示自己闷牌和能否看牌的状态
            if (myIsSeeCard || mySeatUser.State != 1){
                //看牌在动画里面
            }else{
                //没看牌
                var hasSeeCard = that.getMyIsSeeCard();
                if (hasSeeCard){
                    //看牌按钮
                    mo.pszTable.mPszCardLayer.setInhandCardStatus(2);
                }else{
                    //闷牌
                    mo.pszTable.mPszCardLayer.setInhandCardStatus(1);
                }
            }
        }
    };

    //是否有玩家看牌
    that.getIsPlayerSeeCard = function(oldStatus, newStatus){
        if (oldStatus == null || newStatus == null){
            return false;
        }

        for (var seat = 0; seat < 5; seat++){
            var logicSeat = mo.pszTableStatus.getLogicForView(seat, mo.pszTableStatus.mTableStatus.MySeat - 1);
            var seatUserOld = oldStatus["SeatUser" + (logicSeat + 1)];
            var seatUserNew = newStatus["SeatUser" + (logicSeat + 1)];
            if (seatUserOld != null && seatUserNew != null){
                if (seatUserOld.LastAction != null && seatUserNew.LastAction != null){
                    if (seatUserOld.LastAction.code != 0 && seatUserNew.LastAction.code == 0){
                        //看牌
                        if (seat == 0){
                            var mySeatUser = seatUserNew;
                            var cardNums = mySeatUser.HandCards;
                            var cardType = that.getCardType(mySeatUser.Cardtype);
                            mo.pszTable.kanpai(cardNums);
                            mo.pszTable.mPszCardLayer.setInhandCardType(cardType, 1);
                            mo.pszTable.mPszCardLayer.setInhandCardStatus(0);
                        }else{
                            mo.pszTable.setPlayerSeatInhandStatus(seat, 1, 1);
                        }

                        //音效
                        mo.soundHelper.playEffecLookCard();
                        var sex = that.getSexForViewSeat(seat);
                        mo.soundHelper.playPszPeopleKanpai(sex);

                        if (seat == 0 &&
                            newStatus.NowSeat.length > 0 &&
                            newStatus.NowSeat[0] == newStatus.MySeat){
                            //跟注的分数
                            var genzhuScore = that.getMyGenZhuScore();
                            var bipaiScore = that.getMyBipaiScore();
                            mo.pszTable.mPszTableUILayer.setBetBtnStatus(seatUserNew.NewActionList, genzhuScore, bipaiScore);
                        }

                        return true;
                    }
                }
            }
        }

        return false;
    };

    //弃牌
    that.sendQipaiAction = function(){
        if (that.mTableStatus.Step == 1){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            var newActionList = mySeatUser.NewActionList;
            for(var i = 0; i < newActionList.length; i++){
                if (newActionList[i].code == 5){
                    that.sendAction(newActionList[i]);
                    break;
                }
            }
        }
    };

    //比牌
    that.sendBipaiAction = function(seat){
        if (that.mTableStatus.Step == 1){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            var newActionList = mySeatUser.NewActionList;
            for(var i = 0; i < newActionList.length; i++){
                if (newActionList[i].code == 4){
                    var fromSeat = that.mTableStatus.MySeat;
                    var toSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1) + 1;

                    //判断能否比牌
                    if (!that.getIsNotKanPlayerNotBipaiForKanPlayer(toSeat)){
                        mo.showRoomMsgShow("根据所选比牌规则，您不能和已看牌玩家比牌");
                    }else{
                        if (mo.pszTable.mPlayerType == 1){
                            mo.gameMsgManager.sendActionRequestpsz(4, toSeat, fromSeat);
                        }
                    }
                    break;
                }
            }
        }
    };

    //判断没看牌玩家不能跟看牌玩家比牌
    that.getIsNotKanPlayerNotBipaiForKanPlayer = function(toSeat){
        if (mo.pszTable.mGameConfig.ssc == 0){
            return true;
        }

        var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
        var otherSeatUser = that.mTableStatus["SeatUser" + toSeat];

        var myIsSeeCard = (mySeatUser.IsSeeHandCards != null && mySeatUser.IsSeeHandCards != 0);
        var otherIsSeeCard = (otherSeatUser.IsSeeHandCards != null && otherSeatUser.IsSeeHandCards != 0);

        if ((!myIsSeeCard) && otherIsSeeCard){
            return false;
        }

        return true;
    };

    //跟注
    that.sendGenzhuAction = function(){
        if (that.mTableStatus.Step == 1){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            var newActionList = mySeatUser.NewActionList;
            for(var i = 0; i < newActionList.length; i++){
                if (newActionList[i].code == 1 || newActionList[i].code == 2){
                    that.sendAction(newActionList[i]);
                    break;
                }
            }
        }
    };

    //加注
    that.sendJiazhuAction = function(addNum){
        if (that.mTableStatus.Step == 1){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            var newActionList = mySeatUser.NewActionList;
            for(var i = 0; i < newActionList.length; i++){
                if (newActionList[i].code == 3 && newActionList[i].PayNumMin == addNum){
                    that.sendAction(newActionList[i]);
                    break;
                }
            }
        }
    };

    //押注
    that.sendYazhuAction = function(fristNum){
        if (that.mTableStatus.Step == 1){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            var newActionList = mySeatUser.NewActionList;
            for(var i = 0; i < newActionList.length; i++){
                if (newActionList[i].code == 1 && newActionList[i].PayNumMin == fristNum){
                    that.sendAction(newActionList[i]);
                    break;
                }
            }
        }
    };

    //看牌
    that.sendKanpaiAction = function(){
        if (that.mTableStatus.Step == 1){
            if (that.getMyIsSeeCard()){
                if (mo.pszTable.mPlayerType == 1) {
                    mo.gameMsgManager.sendActionRequestpsz(0, 0, 0);
                }
            }
        }
    };

    //发送操作消息
    that.sendAction = function(ActionRequest){
        if (mo.pszTable.mPlayerType == 1) {
            mo.gameMsgManager.sendActionRequestpsz(ActionRequest.code, ActionRequest.PayNumMin, ActionRequest.PayNumMax);
        }
    };

    //获取自己跟注的分数
    that.getMyGenZhuScore = function(){
        var result = 0;

        var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
        var newActionList = mySeatUser.NewActionList;
        for(var i = 0; i < newActionList.length; i++){
            if (newActionList[i].code == 2 || newActionList[i].code == 1){
                result = newActionList[i].PayNumMin;
                break;
            }
        }

        return result;
    };

    //获取自己比牌的分数
    that.getMyBipaiScore = function(){
        var result = 0;

        var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
        var newActionList = mySeatUser.NewActionList;
        for(var i = 0; i < newActionList.length; i++){
            if (newActionList[i].code == 4){
                result = newActionList[i].PayNumMax;
                break;
            }
        }

        return result;
    };

    //转换cardType
    that.getCardType = function(cardType){
        if (cardType == 7){
            return 0;
        }else if (cardType == 6){
            return 1;
        }
        return 6 - cardType;
    };

    //获取第一轮是否可以看牌
    that.getFristRoundSee = function(){
        return false;
    };

    //获取自己是否可以看牌
    that.getMyIsSeeCard = function(){
        var menRound = mo.pszTable.mGameConfig.men;
        var curRound = that.mTableStatus.Round;
        if (menRound < curRound){
            return true;
        }else if (menRound == curRound){
            //大于这一轮才能看牌
            // if (that.mTableStatus.NowSeat.length > 0){
            //     var startSeat = that.mTableStatus.HostSeat - 1;
            //     var curSeat = that.mTableStatus.NowSeat[0] - 1;
            //     var mySeat = that.mTableStatus.MySeat - 1;
            //     var okSeat = [];
            //     var temp = startSeat;
            //     while (((temp + 5) % 5) != curSeat){
            //         var tempSeat = (temp + 5) % 5;
            //         okSeat.push(tempSeat);
            //         temp++;
            //     }
            //     if (mo.gameHelper.findValueForArray(okSeat, mySeat) != null){
            //         return true;
            //     }else{
            //         return false;
            //     }
            // }
        }

        return false;
    };

    //获取可以比牌的玩家列表
    that.getCompariCardSeats = function(){
        var result = [false, false, false, false, false];
        for (var seat = 1; seat < 5; seat++){
            var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
            var seatuser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
            if (seatuser != null && seatuser.State == 1){
                result[seat] = true;
            }
        }
        return result;
    };

    //获取玩家同ip信息
    that.getPlayerIpEqual = function(){
        var result = [];

        //记录ip
        var ipStruct = {};
        for (var i = 1; i <= 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + i];
            if (seatUser != null && seatUser.user_info != null){
                var userInfo = seatUser.user_info;
                if (mo.gameHelper.getIsJson(userInfo.DeviceJson)) {
                    var deviceJson = JSON.parse(userInfo.DeviceJson);
                    var ip = deviceJson.ip;
                    if (ip != null && ip != "" && ip != "127.0.0.1") {
                        if (ipStruct[ip] == undefined) {
                            ipStruct[ip] = [];
                        }
                        ipStruct[ip].push(userInfo.nickname);
                    }
                }
            }
        }

        //分解出出现两次的用户
        for(var key in ipStruct){
            if (ipStruct.hasOwnProperty(key)) {
                if (ipStruct[key].length > 1){
                    var nicks = ipStruct[key];
                    for (var i = 0; i < nicks.length; i++){
                        var item = {};
                        item.nick = nicks[i];
                        item.ip = key;
                        result.push(item);
                    }
                }
            }
        }

        return result;
    };

    //返回当前操作的玩家
    that.getCurrentPlayer = function(){
        var nowLogicSeat = (that.mTableStatus.NowSeat.length > 0) ? that.mTableStatus.NowSeat[0] : 1;
        var seat = that.getViewForLogic(nowLogicSeat - 1, that.mTableStatus.MySeat - 1);
        return seat;
    };

    //返回视图座位号对应的昵称
    that.getViewSeatNick = function(seat){
        var nicks = [null, null, null, null, null];
        for (var i = 0; i < 5; i++){
            var logicseat = that.getLogicForView(i, that.mTableStatus.MySeat - 1);
            var seatUser = that.mTableStatus["SeatUser" + (logicseat + 1)];
            if (seatUser != null){
                nicks[i] = seatUser.user_info.nickname;
            }
        }
        return nicks[seat];
    };

    //获取自己是否准备
    that.getMyIsReady = function(){
        var seatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
        return seatUser.State == 1;
    };

    //获取牌墙状态
    that.getCardWellType = function(){
        return that.mTableStatus.LeftPlayTimes % 2;
        // return 0;
    };

    //通过ui的返回玩家视图座位号
    that.getViewSeatForUid = function (uid) {
        var logicIndex = -1;
        for (var i = 0; i < 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + (i + 1)];
            if (seatUser != null){
                if (seatUser.user_info.userid == uid || seatUser.user_info.nickname == uid){
                    logicIndex = i;
                }
            }
        }
        if (logicIndex != -1){
            return that.getViewForLogic(logicIndex, that.mTableStatus.MySeat - 1);
        }else{
            return -1;
        }
    };

    //通过用户id获取用户性别
    that.getSexForUid = function(uid){
        var sex = 0;
        for (var i = 0; i < 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + (i + 1)];
            if (seatUser != null && seatUser.user_info != null){
                if (seatUser.user_info.userid == uid || seatUser.user_info.nickname == uid){
                    sex = seatUser.user_info.sex;
                }
            }
        }
        return sex;
    };

    //通过视图座位号返回用户性别
    that.getSexForViewSeat = function(view){
        var sex = 0;
        var logicSeat = that.getLogicForView(view, that.mTableStatus.MySeat - 1);
        var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
        if (seatUser != null && seatUser.user_info != null){
            sex = seatUser.user_info.sex;
        }
        return sex;
    };

    //通过逻辑座位号返回用户性别
    that.getSexForLogicSeat = function(seat){
        var sex = 0;
        var seatUser = that.mTableStatus["SeatUser" + (seat + 1)];
        if (seatUser != null && seatUser.user_info != null){
            sex = seatUser.user_info.sex;
        }
        return sex;
    };

    //返回玩家人数
    that.getPlayerCount = function(){
        var count = 0;
        for (var i = 1; i <=5; i++){
            var userSeat = that.mTableStatus["SeatUser" + i];
            if (userSeat != null){
                count++;
            }
        }
        return count;
    };

    //加载录像
    that.setReplayMsg = function(rePlaypsz){
        that.replayMsgs = [];
        that.replayTypes = [];

        //设置录像时候自己的座位号
        var replayMySeat = 1;
        var roundEndedEvent = rePlaypsz.Result;
        for(var i = 1; i <= 5; i++){
            if (roundEndedEvent["SeatUser" + i] != null){
                if (roundEndedEvent["SeatUser" + i].user_info != null){
                    if (roundEndedEvent["SeatUser" + i].user_info.userid == mo.gameUserInfo.uid){
                        replayMySeat = i;
                        break;
                    }
                }
            }
        }
        roundEndedEvent.MySeat = replayMySeat;

        //牌桌映像数据
        for (var i = 0; i < rePlaypsz.Images.length; i++){
            var tableStatspsz = rePlaypsz.Images[i];
            //修改MySeat
            tableStatspsz.MySeat = replayMySeat;

            that.replayMsgs.push(tableStatspsz);
            that.replayTypes.push(2206);
        }

        //牌桌结算数据
        that.replayMsgs.push(rePlaypsz.Result);
        that.replayTypes.push(2208);
    };

    //播放录像
    that.playReplay = function(){
        if (that.replayMsgs.length > 0){
            var msg = that.replayMsgs.shift();
            var type = that.replayTypes.shift();

            var response = null;
            if (type == 2206){
                //牌桌映象
                response = new PszTableStatszjh();

            }else if (type == 2208){
                //牌桌结算
                response = new PszRoundEndedEventzjh();

            }

            if (response){
                response.handleMsg(msg);
                response.log();
            }else{
                cc.warn("type:" + type + "未注册");
            }
            return true;
        }else{
            cc.log("录像播放完毕");
            return false;
        }

    };

    that.getLogicForView = function(view, myLogicSeat){
        var logic = (view + 5 + myLogicSeat) % 5;
        return logic;
    };

    that.getViewForLogic = function(logic, myLogicSeat){
        var view = (logic + 5 - myLogicSeat) % 5;
        return view;
    };

    return that;
};

mo.pszTableStatus = new PszTableStatus();